package com.ptmall.app.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.ptmall.app.R;
import com.ptmall.app.net.HttpMethods;

import java.io.File;

/**
 * Created by Administrator on 2017/3/17.
 */

public class GlideUtil {
    public interface CbGetImg{
        public void onGet(Bitmap img);
    }

    /**
     * 图片重新加载
     * @param context
     */
    public static void resumeShowImg(Context context){
        Glide.with(context).pauseRequests();
    }

    /**
     * 图片暂停加载
     * @param context
     */
    public static void pauseShowImg(Context context){
        Glide.with(context).resumeRequests();
    }
    /**
     * 从url 加载图片
     * @param context
     * @param url
     * @param imageView
     */
    public static void showImg(Context context, String url, ImageView imageView){
        if (!TextUtils.isEmpty(url) && !url.startsWith("http")) {
            url = HttpMethods.BASEIMG_URL + url;
        }
        if (isValidContext(context)) {
            Glide.with(context).load(url).dontAnimate().placeholder(R.drawable.zhanwei2).error(R.drawable.zhanwei).into(imageView);
        } else {
            Log.i("xucc", "Picture loading failed,context is null");
        }
    }
    public static void showKGImg(Context context, String imageUrl, ImageView imageView){
        if (!TextUtils.isEmpty(imageUrl) && !imageUrl.startsWith("http")) {
            imageUrl = HttpMethods.BASEIMG_URL + imageUrl;
        }
        Glide.with(context)
                .load(imageUrl)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        if (imageView == null) {
                            return false;
                        }
                        if (imageView.getScaleType() != ImageView.ScaleType.FIT_XY) {
                            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                        }
                        ViewGroup.LayoutParams params = imageView.getLayoutParams();
                        int vw = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
                        float scale = (float) vw / (float) resource.getIntrinsicWidth();
                        int vh = Math.round(resource.getIntrinsicHeight() * scale);
                        params.height = vh + imageView.getPaddingTop() + imageView.getPaddingBottom();
                        imageView.setLayoutParams(params);
                        return false;
                    }
                })
                .placeholder(R.drawable.zhanwei2)
                .error(R.drawable.zhanwei)
                .into(imageView);
    }

    //判断Activity是否Destroy
    public static boolean isDestroy(Activity activity) {
        if (activity == null || activity.isFinishing() || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed())) {
            return true;
        } else {
            return false;
        }
    }

    public static void showImgSD(Context context, String url, ImageView imageView){
        if (isValidContext(context)) {
            Glide.with(context).load(url).placeholder(R.drawable.plugin_camera_no_pictures).error(R.drawable.plugin_camera_no_pictures).into(imageView);
        } else {
            Log.i("xucc", "Picture loading failed,context is null");
        }
    }
    /**
     * 从localpath/url 加载图片
     * @param context
     * @param url
     * @param imageView
     */
    public static void showAllImg(Context context, String url, ImageView imageView){
        if (isValidContext(context)) {
            Glide.with(context).load(url).placeholder(R.mipmap.logo).error(R.mipmap.logo).into(imageView);
        } else {
            Log.i("xucc", "Picture loading failed,context is null");
        }
    }

    public static boolean isValidContext(final Context context) {
        if (context == null) {
            return false;
        }
        if (context instanceof Activity) {
            final Activity activity = (Activity) context;
            if (activity.isDestroyed() || activity.isFinishing()) {
                return false;
            }
        }
        return true;
    }
    /**
     * uir拼接
     * @param url
     * @return
     */
    public static String getMyUrl(String url){
        try {
            if(!url.startsWith("http")){
                url= HttpMethods.BASE_SITE+url;
            }
        }catch (Exception e){

        }
        return url;
    }
    /**
     * 从url 加载图片
     * @param context
     * @param url
     * @param imageView
     */
    public static void showImgZjh(Context context, String url, ImageView imageView){
        if(!url.startsWith("http")){
            url= HttpMethods.BASEIMG_URL+url;
        }
        if (isValidContext(context)) {
            Glide.with(context).load(url).dontAnimate().placeholder(R.mipmap.logo).error(R.mipmap.logo).into(imageView);
        } else {
            Log.i("xucc", "Picture loading failed,context is null");
        }
    }

    /**
     * 从url 加载图片 并返回图片 Bitmap,如果 不想显示图片 imageView可以为Null
     * @param context
     * @param url
     * @param imageView
     * @param cbGetImg
     */
    public static void showImg(Context context, String url, ImageView imageView, CbGetImg cbGetImg){
        if (!TextUtils.isEmpty(url) && !url.startsWith("http")) {
            url = HttpMethods.BASEIMG_URL + url;
        }
        if (isValidContext(context)) {
            Glide.with(context).load(url).asBitmap().placeholder(R.mipmap.logo).error(R.mipmap.logo).into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    if (null != imageView) {
                        imageView.setImageBitmap(resource);
                    }
                    cbGetImg.onGet(resource);
                }
            });
        } else {
            Log.i("xucc", "Picture loading failed,context is null");
        }
    }

    /**
     * 从资源id加载图片
     * @param context
     * @param res
     * @param imageView
     */
    public static void showImg(Context context, int res, ImageView imageView){
        if (isValidContext(context)) {
            Glide.with(context).load(res).into(imageView);
        } else {
            Log.i("xucc", "Picture loading failed,context is null");
        }
    }

    /**
     * 从文件加载图片
     * @param context
     * @param file
     * @param imageView
     */
    public static void showImg(Context context, File file, ImageView imageView){
        if (isValidContext(context)) {
            Glide.with(context).load(file).into(imageView);
        } else {
            Log.i("xucc", "Picture loading failed,context is null");
        }
    }

    public static String getHttpUrl(String url){
        if (!TextUtils.isEmpty(url) && !url.startsWith("http")) {
            url = HttpMethods.BASE_SITE + url;
        }
        return url;
    }
}
